package com.css.fxfzxczh.modules.eqDisasterLossAssess.houseFinishLossAssess.repository.impl;

import com.css.fxfzxczh.modules.eqDisasterLossAssess.houseFinishLossAssess.entity.*;
import com.css.fxfzxczh.modules.eqDisasterLossAssess.houseFinishLossAssess.repository.HouseFinishLossAssessRepository;
import com.css.fxfzxczh.modules.lossAssessment.resetPrice.entity.ResetPrice;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: fxfzxczh
 * @Author: lhl
 * @CreateDate: 2023/4/24 9:48
 */
@Repository
public class HouseFinishLossAssessRepositoryImpl implements HouseFinishLossAssessRepository {
    @Resource
    JdbcTemplate jdbcTemplate;
    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public List<MhFinishHouseArea> getMhFinishHouseByXsemId(String xsemId) {
        String sql="select * from xczh_middle_high_finish_house_area where xsem_id = ?";
        List<MhFinishHouseArea> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(MhFinishHouseArea.class), xsemId);
        return query;
    }

    @Override
    public int delMhFinishHouseAreaByXsemId(String xsemId) {
        String sql="delete from xczh_middle_high_finish_house_area where xsem_id = ?";
        return jdbcTemplate.update(sql,xsemId);
    }

    @Override
    public void batchSave(List<MhFinishHouseArea> list) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(list.toArray());
        String sql="insert into xczh_middle_high_finish_house_area(xhfmcr_id,xsem_id,xstm_id,xstm_name,total_area,create_user,create_time,concrete_house_area,masonry_house_area1,masonry_house_area2,ratio_type) " +
                "values(:xhfmcrId,:xsemId,:xstmId,:xstmName,:totalArea,:createUser,:createTime,:concreteHouseArea,:masonryHouseArea1,:masonryHouseArea2,:ratioType)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public MhFinishHouseArea findByXstmId(String xstmId) {
        String sql = "select * from  xczh_middle_high_finish_house_area where xstm_id='"+xstmId+"'";
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(MhFinishHouseArea.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public String getRatioTypeByXsemId(String xsemId) {
        String sql = "select DISTINCT(ratio_type) from  xczh_middle_high_finish_house_area where xsem_id='"+xsemId+"'";
        try {
            return jdbcTemplate.queryForObject(sql.toString(), String.class);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }
}
